/**
 * Created by leman on 2017/7/12.
 */
'use strict';


salesCtrl.controller('confirmBillRCtrl', ['$rootScope', '$scope', '$http', '$location', '$stateParams', function ($rootScope, $scope, $http, $location, $stateParams) {
    //配置
    $scope.count = 1; //数据总数
    $scope.p_pernum = 10; //每页显示条数
    //变量
    $scope.p_current = 1;  //当前页数
    $scope.p_all_page = 0;  //总页数
    $scope.pages = [];
    $scope.invoiceStatus =null;
    $scope.billStatus =null;
    //初始化第一页
    getPage($scope.p_current,$scope.p_pernum,function(){
        //alert("我是第一次加载");
    });
    function getPage(page,size,callback){
        // "xxx.html?status=0&page="+page+"&size="+size
        // var billId = 6;
        $http.post("receivable/confirm-list",{
            "pageNo":page,
            "id":$stateParams.data
        }).success(function(res){

            // var thisData= res.data.concat().splice(page*10-10, 10);//当前页数*每页显示条数-每页显示条数,每页显示条数
            /*     if(res&&res.status==1){ */
            var thisData= res.data
            $scope.count=res.data.page.totalRecord;
            $scope.data=thisData;
            $scope.billStatus = thisData.bill.billStatus=='10'?'未对账': (thisData.bill.billStatus=='20'?'已确认' : ( thisData.bill.billStatus=='30'?'已结账':''));
            $scope.p_current = page;
            $scope.p_all_page =Math.ceil($scope.count/$scope.p_pernum);
            reloadPno();//第一次调用并加载
            callback();
            /* }else{
             alert("加载失败");
             }  */
        });
    }
    //单选按钮选中
    $scope.select= function(id){
        //alert(id);
    };
    //首页
    $scope.p_index = function(){
        $scope.load_page(1);
    };
    //尾页
    $scope.p_last = function(){
        $scope.load_page($scope.p_all_page);
    };
    //加载某一页
    $scope.load_page = function(page){
        getPage(page,$scope.p_pernum,function(){});
    };
    //上一页
    $scope.prev_page=function(e){
        if(e.p_current==0){
            return;
        }
        getPage(e.p_current-1,$scope.p_pernum,function(){});
    };
    //下一页
    $scope.next_page=function(e){
        if(e.p_current==$scope.p_all_page){
            return;
        }
        getPage(e.p_current+1,$scope.p_pernum,function(){});
    };
    //跳转到指定页
    $scope.goBtn =function(){
        var idx=$("#on").val();
        getPage(idx,$scope.p_pernum,function(){});
    };
    //初始化页码
    var reloadPno = function(){
        $scope.pages=calculateIndexes($scope.p_current,$scope.p_all_page,8);
    };

    // 移除
    $scope.delBtn = function () {
        var value = this.x.id;
        console.log(value);
        layer.confirm('是否确认移除？', {
            title: '提示'
            , btn: ['确认', '取消'] //按钮
        }, function () {
            $http.post("receivable/delete-rece-detl", {
                "id": value
            }).success(function (res) {
                if (res.success) {
                    getPage($scope.p_current, $scope.p_pernum, function () {
                        layer.msg('已成功移除', {
                            icon: 1
                        });
                    });
                } else {
                    getPage($scope.p_current, $scope.p_pernum, function () {
                        layer.msg('移除失败', {
                            icon: 1
                        });
                    });
                }
            });
        })
    };

    var calculateIndexes = function (current, length, displayLength) {
        var indexes = [];
        var start = Math.round(current - displayLength / 2);
        var end = Math.round(current + displayLength / 2);
        if (start <= 1) {
            start = 1;
            end = start + displayLength - 1;
            if (end >= length - 1) {
                end = length - 1;
            }
        }
        if (end >= length - 1) {
            end = length;
            start = end - displayLength + 1;
            if (start <= 1) {
                start = 1;
            }
        }
        for (var i = start; i <= end; i++) {
            indexes.push(i);
        }
        return indexes;
    };
    var path = getRootPath();
    //调整金额
    $scope.tiaozheng = function (e){
        e.val();
        var keycode= window.event.keyCode;
        if(keycode==13){
            alert(e.val());
        }
    };
    $scope.adjustBtn=function(){
        var id = this.x.id;
        var comment = this.x.comment == null ? "" : this.x.comment;
        var sysCost=this.x.sysCost;//订单金额
        var settleCost=this.x.settleCost;//结算金额
        var alterCost=this.x.alterCost==null ? 0 : this.x.alterCost;//调整金额
        layer.open({
            type: 2,
            title:'调整金额',
            area: ['400px', '380px'], //宽高
            content:"index/adjust?sysCost="+sysCost+"&settleCost="+settleCost+"&alterCost="+alterCost+"&comment="+comment,//传参数：订单价，调整金额，结算价
            btn:["确定","取消"],
            btnAlign: 'c', //按钮居中
            yes:function(index, layero){
                layer.close(index);
                //获取跳转页面的值：订单价，调整金额，结算价，调整页面
                var sysCost = $("#sysCost").val();
                var alterCost = $("#alterCost").val();
                var settleCost = $("#settleCost").val();
                var comment = $("#comment").val();
                // console.log(sysCost)
                // console.log(alterCost)
                // console.log(settleCost)
                // console.log(comment)
                // console.log(id)
                $http.post("receivable/update-detl",{
                    "sysCost":sysCost,
                    "alterCost":alterCost,
                    "settleCost":settleCost,
                    "comment":comment,
                    "id":id
                }).success(function(res){
                    if(res.success) {
                        getPage($scope.p_current, $scope.p_pernum, function () {});
                        layer.msg('修改成功！');
                    }else{
                        layer.msg('修改失败！');
                    }
                });
            }
        });
    };
    //是否开发票
    $scope.BillingOptions = [
        {
            id: 10,
            name: "不开票"
        },
        {
            id: 20,
            name: "待开票"
        },
        {
            id: 30,
            name: "已开票"
        }
    ];

    // 导出
    $scope.export = function () {
        window.location="receivable/export?id="+$stateParams.data;
    };

}]);

